
package ca.abubakrsagar.breadboardsim;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

import javax.swing.JComponent;

/**
 * This class allows for the creation of a breadboard object. This breadboard can contain multiple components on its 64 x 12 grid.
 * The breadboard includes maps for components and voltages to allow for simulations.
 * @author Sagar Desai, Mohammad Abubakr
 * @since May 29, 2012
 */
public class Breadboard extends JComponent {

	private ArrayList<LED> led = new ArrayList<LED>(); // Array list used to store the
												// LED components on the
												// breadboard.
	private ArrayList<Resistor> resistor = new ArrayList<Resistor>(); // Array list used
																// to store the
																// resistors on
																// the
																// breadboard.
	private ArrayList<Switch> switchC = new ArrayList<Switch>(); // Array list used to
															// store the
															// switches on the
															// breadboard.
	private ArrayList<Wire> wire = new ArrayList<Wire>(); // Array list used to store
													// all the wires on the
													// breadboard.

	private double[][] voltMap; // A double dimension array used to map the
								// voltages on the breadboard.
	private Component[][] componentMap; // A double dimension array of
										// components used to keep track of
										// where the components lie on the
										// breadboard.

	/**
	 * Creates a new breadboard while initializing the voltage and component
	 * maps.
	 */
	public Breadboard() {
		// Set the preferred size.
		this.setPreferredSize(new Dimension(660, 440));

		voltMap = new double[14][64];
		componentMap = new Component[14][64];

		// Top and bottom rows are update to be 6 volts because they are rows
		// for power.
		for (int j = 0; j < 64; j++) {
			voltMap[0][j] = 6.0;
			voltMap[13][j] = 6.0;
		}

	}

	/**
	 * @return The specified double value from the voltMap at the given row and
	 *         column.
	 */
	public double getVolts(int row, int column) {
		return voltMap[row][column];
	}

	/**
	 * Sets the voltage at the specified row and column to the given double
	 * value for voltage.
	 */
	public void setVolts(int row, int column, double volts) {
		voltMap[row][column] = volts;
	}

	/**
	 * @return The component in the component map at the specified location.
	 */
	public Component getComponent(int row, int column) {
		return componentMap[row][column];
	}

	/**
	 * Updates the componentMap to have to given component at the specified row
	 * and column.
	 */
	public void setComponent(int row, int column, Component component) {
		componentMap[row][column] = component;

	}

	/**
	 * Handles the drawing of the breadboard. Instead of using the graphics
	 * object, Grahics2D is used by casting g as a Graphics2D object.
	 * 
	 * @param g
	 *            The graphics object
	 */
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;

		g2.drawRect(0, 0, 650, 300); // Initial box.

		// Instanced variables x & y used to control the coordinates where the
		// holes are drawn.
		int x = 7;
		int y = 7;

		for (int i = 0; i < 64; i++) { // draws first two rows
			for (int j = 0; j < 2; j++) {
				g2.drawRect(x, y, 6, 6);
				y += 20;
			}
			y = 7;
			x += 10;
		}
		x = 7;
		y = 57; // leaves space and draws middle section
		for (int i = 0; i < 64; i++) {
			for (int j = 0; j < 10; j++) {
				g2.drawRect(x, y, 6, 6);
				y = y + 20;
			}
			y = 57;
			x = x + 10;
		}

		x = 7;
		y = 267; // leaves space and draws bottom two rows
		for (int i = 0; i < 64; i++) {
			for (int j = 0; j < 2; j++) {
				g2.drawRect(x, y, 6, 6);
				y = y + 20;
			}
			y = 267;
			x = x + 10;
		}

	}

	/**
	 * @return The array list of LEDs.
	 */
	public ArrayList<LED> getLED() {
		return led;
	}

	/**
	 * @return The array list of resistors.
	 */
	public ArrayList<Resistor> getResistor() {
		return resistor;
	}

	/**
	 * @return The array list of switches.
	 */
	public ArrayList<Switch> getSwitch() {
		return switchC;
	}

	/**
	 * @return The array list of wires.
	 */
	public ArrayList<Wire> getWire() {
		return wire;
	}

	

}
